/*
 * Copyright 2014 JootMir Project
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * Support: http://www.jootmir.org
 */
package org.jootmir.client.core.util;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * 抽象数据包类
 * 
 * @author ShawRyan
 */
public abstract class AbstractBundle implements Bundle {

	private HashMap<String, Object> mMap = new HashMap<>();
	
	@Override
	public boolean getBooleanExtra(String key) {
		return getBooleanExtra(key, false);
	}

	@Override
	public boolean getBooleanExtra(String key, boolean defVal) {
		Object o = mMap.get(key);
        if (o == null) {
            return defVal;
        }
        try {
            return (Boolean) o;
        } catch (ClassCastException e) {
            return defVal;
        }
	}

	@Override
	public void putBooleanExtra(String key, boolean val) {
		mMap.put(key, val);
	}

	@Override
	public boolean[] getBooleanArrayExtra(String key) {
		Object o = mMap.get(key);
		if(o == null) return null;
		try {
			return (boolean[])o;
		} catch(ClassCastException e) {
			return null;
		}
	}

	@Override
	public void putBooleanArrayExtra(String key, boolean[] val) {
		mMap.put(key, val);
	}

	@Override
	public byte getByteExtra(String key) {
		return getByteExtra(key, (byte)0);
	}

	@Override
	public Byte getByteExtra(String key, byte defVal) {
		Object o = mMap.get(key);
		if(o == null) return defVal;
		try {
			return (Byte)o;
		} catch (ClassCastException e) {
			return defVal;
		}
	}

	@Override
	public void putByteExtra(String key, byte val) {
		mMap.put(key, val);
	}

	@Override
	public byte[] getByteArrayExtra(String key) {
		Object o = mMap.get(key);
		if(o == null) return null;
		try {
			return (byte[])o;
		} catch (ClassCastException e) {
			return null;
		}
	}

	@Override
	public void putByteArrayExtra(String key, byte[] val) {
		mMap.put(key, val);
	}

	@Override
	public char getCharExtra(String key) {
		return getCharExtra(key, (char)0);
	}

	@Override
	public char getCharExtra(String key, char defVal) {
		Object o = mMap.get(key);
		if(o == null) return defVal;
		try {
			return (Character)o;
		} catch (ClassCastException e) {
			return defVal;
		}
	}

	@Override
	public void putCharExtra(String key, char val) {
		mMap.put(key, val);
	}

	@Override
	public char[] getCharArrayExtra(String key) {
		Object o = mMap.get(key);
		if(o == null) return null;
		try {
			return (char[])o;
		} catch (ClassCastException e) {
			return null;
		}
	}

	@Override
	public void putCharArrayExtra(String key, char[] val) {
		mMap.put(key, val);
	}

	@Override
	public CharSequence getCharSequenceExtra(String key) {
		Object o = mMap.get(key);
		try {
			return (CharSequence)o;
		} catch (ClassCastException e) {
			return null;
		}
	}

	@Override
	public CharSequence getCharSequenceExtra(String key, CharSequence defVal) {
		CharSequence cs = getCharSequenceExtra(key);
		return cs == null ? defVal : cs;
	}

	@Override
	public void putCharSequenceExtra(String key, CharSequence val) {
		mMap.put(key, val);
	}

	@Override
	public CharSequence[] getCharSequenceArrayExtra(String key) {
		Object o = mMap.get(key);
		if(o == null) return null;
		try {
			return (CharSequence[])o;
		} catch (ClassCastException e) {
			return null;
		}
	}

	@Override
	public void CharSequenceArrayExtra(String key, CharSequence[] val) {
		mMap.put(key, val);
	}

	@SuppressWarnings("unchecked")
	@Override
	public ArrayList<CharSequence> getCharSequenceArrayListExtra(String key) {
		Object o = mMap.get(key);
		if(o == null) return null;
		try {
			return (ArrayList<CharSequence>)o;
		} catch (Exception e) {
			return null;
		}
	}

	@Override
	public void putCharSequenceArrayListExtra(String key, ArrayList<CharSequence> val) {
		mMap.put(key, val);
	}

	@Override
	public double getDoubleExtra(String key) {
		return getDoubleExtra(key, 0);
	}

	@Override
	public double getDoubleExtra(String key, double defVal) {
		Object o = mMap.get(key);
		if(o == null) return defVal;
		try {
			return (Double)o;
		} catch (ClassCastException e) {
			return defVal;
		}
	}

	@Override
	public void putDoubleExtra(String key, double val) {
		mMap.put(key, val);
	}

	@Override
	public double[] getDoubleArray(String key) {
		Object o = mMap.get(key);
		if(o == null) return null;
		try {
			return (double[])o;
		} catch (ClassCastException e) {
			return null;
		}
	}

	@Override
	public void putDoubleArray(String key, double[] val) {
		mMap.put(key, val);
	}

	@Override
	public float getFloatExtra(String key) {
		return getFloatExtra(key, 0);
	}

	@Override
	public float getFloatExtra(String key, float defVal) {
		Object o = mMap.get(key);
		if(o == null) return defVal;
		try {
			return (Float)o;
		} catch (ClassCastException e) {
			return defVal;
		}
	}

	@Override
	public void putFloatExtra(String key, float val) {
		mMap.put(key, val);
	}

	@Override
	public float[] getFloatArrayExtra(String key) {
		Object o = mMap.get(key);
		if(o == null) return null;
		try {
			return (float[])o;
		} catch (ClassCastException e) {
			return null;
		}
	}

	@Override
	public void putFloatArrayExtra(String key, float[] val) {
		mMap.put(key, val);
	}

	@Override
	public int getIntExtra(String key) {
		return getIntExtra(key, 0);
	}

	@Override
	public int getIntExtra(String key, int defVal) {
		Object o = mMap.get(key);
		if(o == null) return defVal;
		try {
			return (Integer)o;
		} catch (ClassCastException e) {
			return defVal;
		}
	}

	@Override
	public void putIntExtra(String key, int val) {
		mMap.put(key, val);
	}

	@Override
	public int[] getIntArrayExtra(String key) {
		Object o = mMap.get(key);
		if(o == null) return null;
		try {
			return (int[])o;
		} catch (ClassCastException e) {
			return null;
		}
	}

	@Override
	public void putIntArrayExtra(String key, int[] val) {
		mMap.put(key, val);
	}

	@SuppressWarnings("unchecked")
	@Override
	public ArrayList<Integer> getIntegerArrayListExtra(String key) {
		Object o = mMap.get(key);
		if(o == null) return null;
		try {
			return (ArrayList<Integer>)o;
		} catch (Exception e) {
			return null;
		}
	}

	@Override
	public void putIntegerArrayListExtra(String key, ArrayList<Integer> val) {
		mMap.put(key, val);
	}

	@Override
	public long getLongExtra(String key) {
		return getLongExtra(key, 0);
	}

	@Override
	public long getLongExtra(String key, long defVal) {
		Object o = mMap.get(key);
		if(o == null) return defVal;
		try {
			return (Long)o;
		} catch (ClassCastException e) {
			return defVal;
		}
	}

	@Override
	public void putLongExtra(String key, long val) {
		mMap.put(key, val);
	}

	@Override
	public long[] getLongArrayExtra(String key) {
		Object o = mMap.get(key);
		if(o == null) return null;
		try {
			return (long[])o;
		} catch (ClassCastException e) {
			return null;
		}
	}

	@Override
	public void putLongArrayExtra(String key, long[] val) {
		mMap.put(key, val);
	}

	@Override
	public Serializable getSerializableExtra(String key) {
		Object o = mMap.get(key);
		if(o == null) return null;
		try {
			return (Serializable)o;
		} catch (ClassCastException e) {
			return null;
		}
	}

	@Override
	public void putSerializableExtra(String key, Serializable val) {
		mMap.put(key, val);
	}

	@Override
	public short getShortExtra(String key) {
		return getShortExtra(key, (short) 0);
	}

	@Override
	public short getShortExtra(String key, short defVal) {
		Object o = mMap.get(key);
		if(o == null) return defVal;
		try {
			return (Short)o;
		} catch (ClassCastException e) {
			return defVal;
		}
	}

	@Override
	public void putShortExtra(String key, short val) {
		mMap.put(key, val);
	}

	@Override
	public short[] getShortArrayExtra(String key) {
		Object o = mMap.get(key);
		if(o == null) return null;
		try {
			return (short[])o;
		} catch (ClassCastException e) {
			return null;
		}
	}

	@Override
	public void putShortArrayExtra(String key, short[] val) {
		mMap.put(key, val);
	}

	@Override
	public String getStringExtra(String key) {
		Object o = mMap.get(key);
		if(o == null) return null;
		try {
			return (String)o;
		} catch (ClassCastException e) {
			return null;
		}
	}

	@Override
	public String getStringExtra(String key, String defVal) {
		Object o = mMap.get(key);
		if(o == null) return defVal;
		try {
			return (String)o;
		} catch (ClassCastException e) {
			return defVal;
		}
	}

	@Override
	public void putStringExtra(String key, String val) {
		mMap.put(key, val);
	}

	@Override
	public String[] getStringArrayExtra(String key) {
		Object o = mMap.get(key);
		if(o == null) return null;
		try {
			return (String[])o;
		} catch (ClassCastException e) {
			return null;
		}
	}

	@Override
	public void putStringArrayExtra(String key, String[] val) {
		mMap.put(key, val);
	}

	@SuppressWarnings("unchecked")
	@Override
	public ArrayList<String> getStringArrayListExtra(String key) {
		Object o = mMap.get(key);
		if(o == null) return null;
		try {
			return (ArrayList<String>)o;
		} catch (ClassCastException e) {
			return null;
		}
	}

	@Override
	public void putStringArrayListExtra(String key, ArrayList<String> val) {
		mMap.put(key, val);
	}

}